package com.csdj.s3project.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.csdj.s3project.common.CacheConstant;
import com.csdj.s3project.common.StatusContant;
import com.csdj.s3project.entity.User;
import com.csdj.s3project.mapper.UserMapper;
import com.csdj.s3project.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csdj.s3project.utils.EncryptUtil;
import com.csdj.s3project.utils.RandomUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * <p>
 * 用户信息表 服务实现类
 * </p>
 *
 * @author zq
 * @since 2022-02-04
 */
@Service
public class UserService extends ServiceImpl<UserMapper, User> implements IUserService {

    private Logger log = LoggerFactory.getLogger("userservice");
    @Resource
    private UserMapper userMapper;
    @Resource
    private RedisService redisService;

    /**
     * 用户登录
     *
     * @param account
     * @param password
     * @return 如果登录失败返回null
     */
    @Override
    public User login(String account, String password) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasText(password)) {
            queryWrapper.eq("account", account).eq("password", EncryptUtil.getMd5(password));
            User user = userMapper.selectOne(queryWrapper);
            if (user == null) {
                return null;
            }
            user.setStatus(1);
            if (StatusContant.USER_STATUS_OK == user.getStatus()) {
                //生成token 并保存到redis
                String token = RandomUtil.genToken();
                redisService.setString(CacheConstant.TOKEN_PREFIX + user.getId(), token, CacheConstant.TOKEN_CACHE_TIME);
                user.setToken(token);
            }
            return user;
        }
        return null;
    }

    @Override
    public boolean selectEmail(String email) {
//        log.info(email);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account", email);
        User reEmail = userMapper.selectOne(queryWrapper);
//        log.info(String.valueOf(reEmail));
        if (reEmail == null) {
            return true;
        }
        return false;
    }

    @Override
    public User selectUserInfo(int id) {
        //log.info("id:"+id);
        User userInfo = userMapper.selectById(id);
        if(userInfo == null){
            return null;
        }
        return userInfo;
    }

    @Override
    public int register(User user) {
        String dePass = EncryptUtil.getMd5(user.getPassword());
        user.setPassword(dePass);
        user.setInvitation(RandomUtil.getInvitationCode());
        if (StringUtils.hasText(user.toString()) && selectEmail(user.getEmail())) {
            int result = userMapper.insert(user);
            return result;
        }
        return 0;
    }

    @Override
    public int selectUser(String invitationCode) {
        log.info("invitationCode:"+invitationCode);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("invitation",invitationCode);
        User user = userMapper.selectOne(queryWrapper);
        if(user == null){
            return 0;
        }
        return user.getId();
    }

    @Override
    public int updateSuperiorUser(User user) {
        log.info("sjk中积分为:"+user.getIntegral());
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", user.getId());
        user.setIntegral(user.getIntegral()+20);
        log.info("更新的参数为:"+user);
        int result = userMapper.update(user, updateWrapper);
        if(result == 0){
            return result;
        }
        return result;
    }

    @Override
    public int updatePersonalInfo(User user) {
        log.info("用户想要更新的参数为:"+user);
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", user.getId());
        int result = userMapper.update(user, updateWrapper);
        if(result == 0){
            return result;
        }
        return result;
    }

}
